package com.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.system.pojo.*;
import com.system.server.CourseService;
import com.system.server.TeacherService;
import com.system.server.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: E-mail:
 * @Version: 创建时间：2019年04月11日11:28
 * @Description: <描述>
 */
@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Resource
    private TypeService typeService;
    @Autowired
    private TeacherService teacherService;
    @Resource
    private CourseService courseService;

    @ModelAttribute("roleName")
    public String userType(HttpSession session) {
        UserLogin userLogin = (UserLogin) session.getAttribute("userLogin");

        Role role = typeService.findRoleByUserLoginName(userLogin.getUsername());
        String roleName = role.getRoleName();

        return roleName;
    }
    @RequestMapping("/findAll")
    public String findAll(Integer pageNum, Model model, HttpSession session) {
        if(ObjectUtils.isEmpty(pageNum)) {
            //设置页码
            pageNum = 1;
        }
        //获取老师的id
        UserLogin userLogin = (UserLogin) session.getAttribute("userLogin");
        String username = userLogin.getUsername();

        PageHelper.startPage(pageNum,5);

        List<Course> lists = teacherService.findCourseById(username);

        PageInfo<Course> pageInfo = new PageInfo<>(lists);
        model.addAttribute("pageInfo",pageInfo);

        return "/teacher/showCourse";
    }
    @RequestMapping("/selectCourseByName")
    public String selectCourseByName(String findByName,Integer pageNum,Model model, HttpSession session) {
        if(ObjectUtils.isEmpty(pageNum)) {
            //设置页码
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,5);
        //获取老师的id
        UserLogin userLogin = (UserLogin) session.getAttribute("userLogin");

        List<Course> lists = courseService.findCourseByNameAndTeacherName(findByName,userLogin.getUsername());

        PageInfo<Course> pageInfo = new PageInfo<>(lists);

        model.addAttribute("pageInfo",pageInfo);
        //数据回显
        model.addAttribute("conditionName",findByName);
        return "/teacher/showCourse";
    }
    @RequestMapping("/gradeCourse")
    public String gradeCourse(Integer id,Integer pageNum,Model model) {
        if(ObjectUtils.isEmpty(pageNum)) {
            //设置页码
            pageNum = 1;
        }
        PageHelper.startPage(pageNum,5);

        List<SelectedCourseDTO> lists = teacherService.findSelectedCourse(id);

        PageInfo<SelectedCourseDTO> pageInfo = new PageInfo<>(lists);

        model.addAttribute("pageInfo",pageInfo);

        return "/teacher/showGrade";
    }
    @RequestMapping("/showMsg")
    public String mark(Integer studentid,Integer courseid,Model model) {

        SelectedCourseDTO stu = teacherService.findSelectedCourseStu(courseid,studentid);

        model.addAttribute("selectedCourse",stu);

        return "teacher/mark";
    }
    @RequestMapping("/mark")
    public String mark(CourseVO courseVO) {
        teacherService.modifyMark(courseVO);

        return "redirect:/teacher/findAll";
    }

}
